import { IBreadCurmb } from '@/base-ui/BreadCrumb/types'
import { RouteRecordRaw } from 'vue-router'
let firstMenu: any = null

export function mapMenusRouter(userMenus: any[]): RouteRecordRaw[] {
  const routes: RouteRecordRaw[] = []
  //先去加载默认所有的routes
  const allRoutes: RouteRecordRaw[] = []
  const routerFiles = require.context('../router/main', true, /\.ts/)
  // console.log(routerFiles)
  routerFiles.keys().forEach((key) => {
    const router = require('../router/main' + key.split('.')[1])
    // console.log(router)
    // console.log(router.default)
    allRoutes.push(router.default)
  })
  // console.log(allRoutes)
  const _recurseGetRoute = (menus: any[]) => {
    for (const menu of menus) {
      const route = allRoutes.find((route) => route.path === menu.url)
      if (route) {
        routes.push(route)
      } else {
        _recurseGetRoute(menu.children)
      }
      if (!firstMenu) {
        firstMenu = menu
      }
    }
  }
  _recurseGetRoute(userMenus)
  // console.log(routes)
  return routes
}
export function pathMapBreadCurmbs(userMenus: any[], currentPath: string) {
  const breadCrumbs: IBreadCurmb[] = []
  pathMapMenu(userMenus, currentPath, breadCrumbs)
  return breadCrumbs
}
export function pathMapMenu(
  userMenus: any[],
  currentPath: string,
  breadCrumbs?: IBreadCurmb[]
): any {
  for (const menu of userMenus) {
    if (menu.type === 1) {
      const findMenu = pathMapMenu(menu.children ?? [], currentPath)
      if (findMenu) {
        breadCrumbs?.push({ name: menu.name })
        breadCrumbs?.push({ name: findMenu.name })
        return findMenu
      }
    } else if (menu.type === 2 && menu.url === currentPath) {
      return menu
    }
  }
}

export function mapMenusToPermissions(userMenus: any[]) {
  const permissions: string[] = []

  const _recurseGetPermission = (menus: any[]) => {
    for (const menu of menus) {
      if (menu.type === 1 || menu.type === 2) {
        _recurseGetPermission(menu.children ?? [])
      } else if (menu.type === 3) {
        permissions.push(menu.permission)
      }
    }
  }
  _recurseGetPermission(userMenus)

  return permissions
}

export function menuMapLeafKeys(menuList: any[]) {
  const leftKeys: number[] = []

  const _recurseGetLeaf = (menuList: any[]) => {
    for (const menu of menuList) {
      if (menu.children) {
        _recurseGetLeaf(menu.children)
      } else {
        leftKeys.push(menu.id)
      }
    }
  }
  _recurseGetLeaf(menuList)

  return leftKeys
}

export function menuMapParentId(menuList: any[], type: number) {
  const parentIdOptions: any[] = []
  const _recurseGetLeaf = (menuList: any[]) => {
    // debugger
    for (const menu of menuList) {
      if (menu.children && menu.type !== type) {
        _recurseGetLeaf(menu.children)
      } else {
        const obj: any = {}
        obj.title = menu.name
        obj.value = menu.id
        parentIdOptions.push(obj)
      }
    }
  }
  // debugger
  _recurseGetLeaf(menuList)
  return parentIdOptions
}
export { firstMenu }
